package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    /*文件上传*/
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        log.info("文件上传：{}",file);

        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            //将地址保存到redis
            SetOperations setOperations = redisTemplate.opsForSet();
            setOperations.add(RedisConstant.SETMEAL_PIC_RESOURCES,filePath);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }

        return new  Result(false,MessageConstant.PIC_UPLOAD_FAIL,null);
    }

    /*新增套餐*/
    @PostMapping("/add/{checkgroupIds}")
    public Result add(@RequestBody Setmeal setmeal, @PathVariable List<Long> checkgroupIds){
        log.info("新增套餐：{}", setmeal);
        setmealService.add(setmeal,checkgroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS,null);
    }
    /*修改套餐*/
    @PostMapping("/edit/{checkGroupIds}")
    public Result edit(@RequestBody Setmeal setmeal,@PathVariable List<Long> checkGroupIds){
        log.info("修改套餐：{}", setmeal);
        setmealService.edit(setmeal,checkGroupIds);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS,null);
    }
    /*根据id查询套餐*/
    @GetMapping("/findById")
    public Result findById(Long id){
        Setmeal setmeal=setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }
    /*分页查询*/
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询：{}", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

}
